h_addrtype As Integer 'Winsock also palces the data
h_length As Integer 'pointed to by these pointers
h_addr_list As Long 'at the end of the hostent data.
buff As String * 100 'The data 'pointed' to by the
'above pointers.
End Type
Global namehostent As namehostent_type
Type sockopt_bool_type 'Used for setting socket options
val As Integer
End Type
Global sockopt_bool As sockopt_bool_type
'Winsock calls in VB format
Declare Function bind Lib "winsock.dll" (ByVal s As Integer, addr As sockaddr_in_type, ByVal namelen As Integer) As Integer
Declare Function inet_addr Lib "winsock.dll" (ByVal s As String) As Long
Declare Function gethostbyname Lib "winsock.dll" (ByVal hostname As String) As Long
Declare Function gethostbyaddr Lib "winsock.dll" (hostaddress As Long, ByVal lenaddress As Integer, ByVal pftype As Integer) As Long
Declare Function inet_ntoa Lib "winsock.dll" (ByVal iaddr As Long) As Long
Declare Function socket Lib "winsock.dll" (ByVal af As Integer, ByVal typesock As Integer, ByVal protocol As Integer) As Integer
Declare Function connect Lib "winsock.dll" (ByVal sock As Integer, sockstruct As sockaddr_in_type, ByVal structlen As Integer) As Integer
Declare Function send Lib "winsock.dll" (ByVal sock As Integer, ByVal msg As String, ByVal msglen As Integer, ByVal flag As Integer) As Integer
Declare Function recv Lib "winsock.dll" (ByVal sock As Integer, ByVal msg As String, ByVal msglen As Integer, ByVal flag As Integer) As Integer
Declare Function closesocket Lib "winsock.dll" (ByVal sock As Integer) As Integer
Declare Function setsockopt Lib "winsock.dll" (ByVal sock As Integer, ByVal level As Integer, ByVal optname As Integer, optval As sockopt_bool_type, ByVal optlen As Integer) As Integer
Declare Function htons Lib "winsock.dll" (ByVal a As Integer) As Integer
'These are the Win specific calls which use messages
Declare Function WSAStartup Lib "winsock.dll" (ByVal a As Integer, b As WSAdata_type) As Integer
Declare Function WSACleanup Lib "winsock.dll" () As Integer
Declare Function WSAAsyncSelect Lib "winsock.dll" (ByVal sock As Integer, ByVal hWnd As Integer, ByVal msg As Integer, ByVal event As Long) As Integer
Declare Function WSAGetLastError Lib "winsock.dll" () As Integer
Declare Function WSAAsyncGetHostByAddr Lib "winsock.dll" (ByVal hWnd As Integer, ByVal msg As Integer, hostaddr As Long, ByVal lenhostaddr As Integer, ByVal pftype As Integer, namehostent As namehostent_type, ByVal lenstruc As Integer) As Integer
Declare Function WSAAsyncGetHostByName Lib "winsock.dll" (ByVal hWnd As Integer, ByVal msg As Integer, ByVal hostname As String, namehostent As namehostent_type, ByVal lenanmehostent As Integer) As Integer
'Some constants declarations
Global Const SOCKET_ERROR = -1
Global Const INVALID_SOCKET = -1
Global Const SOCK_STREAM = 1
Global Const AF_INET = 2
Global Const PF_INET = 2
Global Const IPPROTO_TCP = 6
Global Const SOL_SOCKET = &HFFFF
Global Const SO_DEBUG = &H1
Global Const SO_ACCEPTCONN = &H2
Global Const SO_REUSEADDR = &H4
Global Const SO_KEEPALIVE = &H8
Global Const SO_DONTROUTE = &H10
Global Const SO_BROADCAST = &H20
Global Const SO_USELOOPBACK = &H40
Global Const SO_LINGER = &H80
Global Const SO_OOBINLINE = &H100
Global Const SO_DONTLINGER = &HFF7F
Global Const AF_UNSPEC = 0
Global Const FD_READ = 1
Global Const FD_WRITE = 2
Global Const FD_OOB = 4
Global Const FD_ACCEPT = 8
Global Const FD_CONNECT = &H10
Global Const FD_CLOSE = &H20
Function ask_server (req_msg$)
DoEvents
mlen = Len(req_msg$)
'Clear the inbound message buffer
current_msg$ = ""
dprint "Send request"
send_next_segment:
status% = send(callsocket%, req_msg$, mlen, 0)
If status% = SOCKET_ERROR Then
status% = WSAGetLastError()
dprint "Send ERROR " & sockerror$(status%)
GoTo exit_ask_server
ElseIf status% = mlen Then
dprint "Send was OK...waiting for response."
Else
dprint "Partial send of " & mlen & " bytes"
req_msg$ = Mid$(req_msg$, status% + 1)
mlen = Len(req_msg$)
GoTo send_next_segment
End If
'Now wait for the response from the server.
'Keep trying to receive until the server disconnects.
'At that time the receive will fail
Do
DoEvents
'We should also place a timeout routine...just in
'case. I'll let you write that.
If quit_flag Then
Exit Do
End If
If closed Then
'Msgblast routine dprints the close message and
'sets the global flag 'closed'
dprint "Closed received"
Exit Do
End If
Loop
status% = close_sock(callsocket%)
callsocket% = 0
'Current_msg$ is global and is where the receive event